from event_rule.dwc import NO_BET_AMOUNT_GAME_TYPE, NO_WIN_AMOUNT_GAME_TYPE

GAME_AGGREGATE_REPORT = {
    'group_by': '_chn_device_type.keyword',
    'real_time_report': False,
    'dynamic_argument': True,
    'interval_type_list': ['day', 'hour'],
    'target_event_id': ['bet_order', 'recharge'],
    'values': [
        {
            "value_id": "participate_user",
            "target_value": "_user_id",
            "value_name": "投注账号",
            "filter": [
                {"term": {"_event_id": "bet_order"}}
            ],
            "value_type": "long",
            "operation": "cardinality",
            "description": "当日具下注行为的用户数量"
        },
        {
            "value_id": "recharge_bet_user",
            "target_value": "_user_id",
            "value_name": "付费投注账号",
            "filter": [
                {"term": {"_event_id": "bet_order"}},
                {"terms": {"_user_id": "${recharge_user}"}}
            ],
            "value_type": "long",
            "operation": "cardinality",
            "description": "投注用户中当日有付费行为的账号数量"
        },
        {
            "value_id": "new_bet_user",
            "target_value": "_user_id",
            "value_name": "首次投注账号",
            "filter": [
                {"terms": {"_user_id": "${first_bet_user}"}}
            ],
            "value_type": "long",
            "operation": "cardinality",
            "hide_result": True,
            "description": "当日前未进入游戏任一房间,\n于当日参与投注的用户数,\n各房间独立计算，合计时去重"
        },
        {
            "value_id": "new_recharge_bet_user",
            "target_value": "_user_id",
            "value_name": "首投付费账号",
            "filter": [
                {"term": {"_event_id": "recharge"}},
                {"terms": {"_user_id": "${first_bet_user}"}}
            ],
            "value_type": "long",
            "operation": "cardinality",
            "hide_result": True,
            "description": "当日前未进入游戏任一房间，\n于当日充值并参与投注的用户数，\n各房间独立计算，合计时去重"
        },
        {
            "value_id": "bet_count",
            "target_value": "bet_order_bet_amount",
            "value_name": "投注次数",
            "filter": [],
            "operation": "value_count",
            "value_type": "long",
            "description": "游戏投注次数，以局为单位"
        },
        {
            "value_id": "_bet_amount",
            "target_value": "bet_order_bet_amount",
            "value_name": "投注金额",
            "filter": [
                {'terms': {'_event_id': ['bet_order']}}
            ],
            "value_type": "float",
            "adjust_amount": True,
            "hide_result": True
        },
        {
            "value_id": "lose_award_amount",
            "target_value": "bet_order_award_amount",
            "value_name": "负扣金额",
            "filter": [
                {"terms": {"bet_order_sub_type.keyword": NO_BET_AMOUNT_GAME_TYPE}},
                {"range": {"bet_order_award_amount": {"lt": 0}}}
            ],
            "value_type": "float",
            "hide_result": True,
            "adjust_amount": True,
        },
        {
            "value_id": "invalid_bet_amount",
            "target_value": "bet_order_bet_amount",
            "value_name": "无效投注",
            "filter": [
                {"terms": {"bet_order_sub_type.keyword": NO_WIN_AMOUNT_GAME_TYPE}},
                {"range": {"bet_order_award_amount": {"gt": 0}}}
            ],
            "value_type": "float",
            "hide_result": True,
            "adjust_amount": True,
        },
        {
            "value_id": "bet_amount",
            "value_name": "投注金额",
            "value_expr": "_bet_amount - lose_award_amount",
            "adjust_amount": True,
            "description": "游戏投注金额总和",
        },
        {
            "value_id": "valid_bet_amount",
            "value_name": "有效投注",
            "value_expr": "_bet_amount - invalid_bet_amount + (lose_award_amount * -1)",
            "adjust_amount": True,
            "description": "斗地主/血拼牛牛：”赢家”投注金额总和，\n诈金花/梭哈：”输家”投注金额总和，\n其余游戏等同投注金额"
        },
        {
            "value_id": "_award_amount",
            "target_value": 'bet_order_award_amount',
            "value_name": "中奖金额(税后)",
            "filter": [],
            "value_type": "float",
            "hide_result": True,
            "adjust_amount": True,
        },
        {
            "value_id": "award_amount",
            "value_name": "中奖金额(税后)",
            "value_expr": "_award_amount - lose_award_amount",
            "adjust_amount": True,
            "description": "游戏中赢家的派奖金额，并扣除税收",
        },
        {
            "value_id": "tax_amount",
            "target_value": 'bet_order_tax_amount',
            "value_name": "玩法税收",
            "filter": [],
            "value_type": "float",
            "adjust_amount": True,
            "description": "游戏中赢家所产生的固定比例手续费"
        },
        {
            "value_id": "high_level_fish_bet_amount",
            "target_value": 'bet_order_bet_amount',
            "target_index": ["dark3", 'dark2', 'dark4'],
            "value_name": "高级鱼房投注金额",
            "filter": [
                {"term": {"bet_order_sub_type.keyword": "fish"}},
                {"terms": {"bet_order_third_type.keyword": ["fish_3", "fish_4", "3", "4"]}}
            ],
            "value_type": "float",
            "hide_result": True,
            "adjust_amount": True,
        },
        {
            "value_id": "channel_tax_amount",
            "target_index": ["dark3", 'dark2', 'dark4'],
            "value_name": "渠道税收",
            "value_expr": "tax_amount + high_level_fish_bet_amount * 0.015",
            "adjust_amount": True,
            "description": "以渠道为维度之总和，包含捕鱼等游戏的隐藏税收"
        },
        {
            "value_id": "_choushui",
            "target_value": ['bet_order_bet_amount', '-', 'bet_order_award_amount'],
            "value_name": "抽水金额",
            "filter": [
                {'terms': {'_event_id': ['bet_order']}}
            ],
            "value_type": "float",
            "hide_result": True,
            "adjust_amount": True,
        },
        {
            "value_id": "tax_percentage",
            "value_name": "玩法税收占比",
            "value_expr": "tax_amount/_choushui",
            "is_percentage": True,
            "fmt": lambda v: float("%.2f" % (v * 100)),
            "description": "玩法税收／玩法营收"

        },
        {
            "value_id": "choushui",
            "value_name": "玩法营收",
            "value_expr": "_choushui",
            "adjust_amount": True,
            "description": "投注金额 - 中奖金额"
        },
        {
            "value_id": "choushui_rate",
            "value_name": "抽水率",
            "value_expr": "choushui/valid_bet_amount",
            "is_percentage": True,
            "fmt": lambda v: float("%.2f" % (v * 100)),
            "description": "玩法营收 / 有效投注金额"
        },
        {
            "value_id": "profit_user_count",
            "target_value": "_user_id",
            "value_name": "盈利用户",
            "filter": [
                {"terms": {"_user_id": "${profit_user_pool}"}}
            ],
            "operation": "cardinality",
            "value_type": "long",
            "description": "当日中奖金额 - 投注金额为正数的玩家数量"
        },
        {
            "value_id": "profit_user_profit_amount",
            "target_value": ['bet_order_award_amount', '-', 'bet_order_bet_amount'],
            "value_name": "赢家盈亏",
            "filter": [
                {"term": {"_event_id": "bet_order"}},
                {"terms": {"_user_id": "${profit_user_pool}"}}
            ],
            "value_type": "float",
            "adjust_amount": True,
            "description": "当日赢家盈利金额总和"
        },
        {
            "value_id": "lose_user_count",
            "target_value": "_user_id",
            "value_name": "亏损用户",
            "filter": [
                {"terms": {"_user_id": "${lose_user_pool}"}}
            ],
            "operation": "cardinality",
            "value_type": "long",
            "description": "当日中奖金额 - 投注金额为负数的玩家数量"
        },
        {
            "value_id": "lose_user_lose_amount",
            "target_value": ['bet_order_award_amount', '-', 'bet_order_bet_amount'],
            "value_name": "输家盈亏",
            "filter": [
                {"term": {"_event_id": "bet_order"}},
                {"terms": {"_user_id": "${lose_user_pool}"}}
            ],
            "value_type": "float",
            "adjust_amount": True,
            "description": "当日输家亏损金额总和"
        }
    ]
}
